﻿''' <summary>
''' 日付列属性
''' </summary>
''' <remarks>
''' 時刻分は無視します。
''' </remarks>
Public Class StringDateColumnAttribute
    Inherits ColumnAttribute

    Public Property RelationFormat As String = "yyyyMMdd"
    Public Property ObjectFormat As String = "0000/00/00"

    Public Overrides Sub SetValue(model As Object, val As Object)
        If IsDBNull(val) Then
            MyBase.SetValue(model, val)
        Else
            MyBase.SetValue(model, Date.Parse(Format(CInt(val), Me.ObjectFormat)))
        End If
    End Sub

    Public Overrides Function GetValue(model As Object) As Object
        Dim t As Type = Me.GetPropertyType
        If t.Equals(GetType(DateTime)) = False Then Return MyBase.GetValue(model)

        Dim val As DateTime? = MyBase.GetValue(model)
        Return If(val Is Nothing, val, val.Value.ToString(Me.RelationFormat))
    End Function

End Class
